const sharp = require("sharp");

module.exports = {
  // buffer格式压缩
  handleSharpBuffer(buffer) {
    return new Promise((resolve, reject) => {
      sharp(buffer)
        .jpeg({ quality: 60 })
        .png({ quality: 60 })
        .toBuffer()
        .then((compressedBuffer) => {
          resolve(compressedBuffer);
        })
        .catch((err) => {
          console.error("图片压缩过程中发生错误:", err);
          reject(err);
        });
    });
  },
  // buffer格式压缩
  handleLinkBuffer(url) {
    return new Promise((resolve, reject) => {
      sharp(url)
        .jpeg({ quality: 60 })
        .png({ quality: 60 })
        .then((compressedBuffer) => {
          console.log(compressedBuffer, "compressedBuffer");
          resolve(compressedBuffer);
        })
        .catch((err) => {
          console.error("图片压缩过程中发生错误:", err);
          reject(err);
        });
    });
  },

  // svg 链接 格式压缩
  SVGLinkBuffer(url) {
    return new Promise((resolve, reject) => {
      sharp(url)
        .png()
        .toBuffer()
        .then((compressedBuffer) => {
          console.log(compressedBuffer, "SVGLinkBuffer");
          resolve(compressedBuffer);
        })
        .catch((err) => {
          console.error("图片转换压缩过程中发生错误:", err);
          reject(err);
        });
    });
  },

  // ico 链接 格式压缩
  ICOLinkBuffer(url) {
    return new Promise((resolve, reject) => {
      sharp(url)
        .png()
        .toFile("output.png")
        .then((compressedBuffer) => {
          console.log(compressedBuffer, "ICOLinkBuffer");
          resolve(compressedBuffer);
        })
        .catch((err) => {
          console.error("图片转换压缩过程中发生错误:", err);
          reject(err);
        });
    });
  },
};
